package com.huarui.user.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import com.huarui.user.dao.IUserDao;
import com.huarui.user.model.User;

public class UserDaoImpl extends BaseDaoImpl implements IUserDao {

	@SuppressWarnings("unchecked")
	public List<User> getUserByName(String name) {
		return (List<User>) getSqlMapClientTemplate().queryForList(namespace + ".getUserByName", name);
	}
	
	public User getUserByEmail(String email) {
		return (User) getSqlMapClientTemplate().queryForObject(namespace + ".getUserByEmail", email);
	}
	
	public User getByActiveCode(String activateCode) {
		return (User) getSqlMapClientTemplate().queryForObject(namespace + ".getByActiveCode", activateCode);
	}
	

	@SuppressWarnings("unchecked")
	public boolean updateUserStatus(Integer id, String status) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("status", status);
		
		return getSqlMapClientTemplate().update(namespace + ".updateUserStatus", map) == 1;
	}
	
	@SuppressWarnings("unchecked")
	public boolean updateUserPassword(Integer id, String password) {
		Map map = new HashMap();
		map.put("id", id);
		map.put("password", password);
		
		return getSqlMapClientTemplate().update(namespace + ".updateUserPassword", map) == 1;
	}

	@SuppressWarnings("unchecked")
	public List<Map> countByCreated() {
		return (List<Map>) getSqlMapClientTemplate().queryForList(namespace + ".countByCreated");
	}

	@SuppressWarnings("unchecked")
	public List<String> getEmailsByCreated(List<String> ymCreated) {
		return (List<String>) getSqlMapClientTemplate().queryForList(namespace + ".getEmailsByCreated", ymCreated);
	}
	
	public boolean rejectEmail(String email) {
		return getSqlMapClientTemplate().update(namespace + ".rejectEmail", email) == 1;
	}

	@SuppressWarnings("unchecked")
	@Override
    public List<User> getUserByPyCode(String pyCode) {
		
		return	getSqlMapClientTemplate().queryForList(namespace+".getUserByPyCode",pyCode);
	}
	
	@Override
	public User getUserByIdent(String ident) {		
		return  (User)getSqlMapClientTemplate().queryForObject(namespace+".getUserByIdent",ident);
	}
	
	@Override
	public User getUserByActiveCode(String activeCode) {
			
		return  (User)getSqlMapClientTemplate().queryForObject(namespace+".getUserByActiveCode",activeCode);
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getUsersByPYCodePrefix(
			String PYCodePrefix, int start, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pyCode", PYCodePrefix + "%");
		params.put("start", start);
		params.put("pageSize", pageSize);
		return (List<Map<String, Object>>) this.getSqlMapClientTemplate()
				.queryForList(namespace + ".getUserByPYCodePrefix", params);
	}

	@Override
	public int getUsersCountByPYCodePrefix(String PYCodePrefix) {
		String pyCode = PYCodePrefix + "%";
		return (Integer) this.getSqlMapClientTemplate().queryForObject(
				namespace + ".getUserCountByPYCodePrefix", pyCode);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map> getUser4MapByConditions(Map<String, Object> map) {		
		return getSqlMapClientTemplate().queryForList(namespace+".getUser4MapByConditions", map);
	}

	@Override
	public Integer getUserCountByConditions(Map<String, Object> map) {		
		return (Integer) getSqlMapClientTemplate().queryForObject(namespace+".getUserCountByConditions", map);
	}
		
}
